package com.hilaver.dzmis.service.impl;

import java.util.List;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.basicinfo.BiYarn;
import com.hilaver.dzmis.codetable.CtCurrency;
import com.hilaver.dzmis.codetable.CtSeasonNumber;
import com.hilaver.dzmis.codetable.CtYarnMaterial;
import com.hilaver.dzmis.codetable.CtYarnMeasure;
import com.hilaver.dzmis.quotation.QuYarn;
import com.hilaver.dzmis.service.AbstractBaseService;
import com.hilaver.dzmis.service.IEntityService;
import com.hilaver.dzmis.util.SimpleObj2XML;
import com.hilaver.dzmis.util.StringUtils;

public class QuYarnServiceImpl extends AbstractBaseService implements
		IEntityService {
	
	private CtServiceImpl ctService;
	
	public QuYarnServiceImpl(){
		this.ctService = new CtServiceImpl();
	}
	
	@Override
	public String delete(int id) throws Exception {
		return super.delete(QuYarn.class.getName(), id);
	}

	@Override
	public String get(int id) throws Exception {
		return super.get(QuYarn.class.getName(), id);
	}

	@Override
	public String getAllPagination(int page, String sort, String order)
			throws Exception {
		return super.getAllPagination(QuYarn.class.getName(), page, sort, order);
	}
	
	public String getAll(String sort, String order) throws Exception{
		List<QuYarn> objList = this.dao.getAll(QuYarn.class.getName(), sort, order);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (QuYarn obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(objList.size());
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);

		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String getAllPagination(int page, String sort, String order, String[] filters)
			throws Exception {
		int offset = Constants.PAGE_SIZE * (page - 1);
		List<QuYarn> objList = this.dao.getAllPagination(QuYarn.class.getName(), offset,
				Constants.PAGE_SIZE, sort, order, filters);

		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (QuYarn obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);
			xmlSB.append(this.toXMLFragment(obj));
			xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(page);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(QuYarn.class.getName(), filters));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);

		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

	public String toXMLFragment(QuYarn quYarn) throws Exception{
		StringBuffer sb = new StringBuffer();
		
		BiYarn biYarn = quYarn.getBiYarn();
		
		sb.append("<ctSeasonNumber>");
		sb.append(Constants.XML_CDATA[0]);
		sb.append(this.ctService.getDescription(CtSeasonNumber.class.getName(), quYarn.getCtSeasonNumber(), locale));
		sb.append(Constants.XML_CDATA[1]);
		sb.append("</ctSeasonNumber>");
		
		sb.append("<ctCurrency>");
		sb.append(Constants.XML_CDATA[0]);
		sb.append(this.ctService.getDescription(CtCurrency.class.getName(), quYarn.getCtCurrency(), locale));
		sb.append(Constants.XML_CDATA[1]);
		sb.append("</ctCurrency>");
		
		sb.append("<yarnReference>");
		sb.append(Constants.XML_CDATA[0]);
		sb.append(biYarn.getReference());
		sb.append(Constants.XML_CDATA[1]);
		sb.append("</yarnReference>");
		
		
		sb.append("<yarnAccount>");
		sb.append(Constants.XML_CDATA[0]);
		sb.append(this.ctService.getDescription(CtYarnMeasure.class.getName(), biYarn.getCtYarnMeasure(), locale) + "&nbsp;&nbsp;" + biYarn.getYarnAccount());
		sb.append(Constants.XML_CDATA[1]);
		sb.append("</yarnAccount>");
		
		sb.append("<ctYarnMaterial>");
		sb.append(Constants.XML_CDATA[0]);
		sb.append(this.ctService.getDescription(CtYarnMaterial.class.getName(), biYarn.getCtYarnMaterial1(), locale));
		if(!StringUtils.isEmpty(biYarn.getCtYarnMaterial1()) && biYarn.getComposition1() != null){
			sb.append("&nbsp;(&nbsp;" + biYarn.getComposition1() + "%&nbsp;)");
		}
		sb.append(Constants.XML_CDATA[1]);
		sb.append("</ctYarnMaterial>");
		
		if(biYarn.getBiFactory() != null){
			sb.append("<factoryNameCN>");
			sb.append(Constants.XML_CDATA[0]);
			sb.append(biYarn.getBiFactory().getNameCN());
			sb.append(Constants.XML_CDATA[1]);
			sb.append("</factoryNameCN>");
			
			sb.append("<factoryNameEN>");
			sb.append(Constants.XML_CDATA[0]);
			sb.append(biYarn.getBiFactory().getNameEN());
			sb.append(Constants.XML_CDATA[1]);
			sb.append("</factoryNameEN>");
			
			sb.append("<addressFactoryCN>");
			sb.append(Constants.XML_CDATA[0]);
			sb.append(biYarn.getBiFactory().getAddressFactoryCN());
			sb.append(Constants.XML_CDATA[1]);
			sb.append("</addressFactoryCN>");
			
			sb.append("<addressFactoryEN>");
			sb.append(Constants.XML_CDATA[0]);
			sb.append(biYarn.getBiFactory().getAddressFactoryEN());
			sb.append(Constants.XML_CDATA[1]);
			sb.append("</addressFactoryEN>");
		}
		
		return sb.toString();
	}
	
}
